package com.beiyou.orderingsystemonweb.system.controller;

import com.beiyou.orderingsystemonweb.system.domain.Login;
import com.beiyou.orderingsystemonweb.system.domain.Result;
import com.beiyou.orderingsystemonweb.system.service.LoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * 用户登录
 * 1.普通用户
 * 2.店家
 */
@Controller
public class LoginController {
    //跳转到登录页面
    //处理"/"地址栏的请求
    @RequestMapping("/")
    public String login(HttpServletRequest request){
        //检查cookie
        Cookie[] loginCookies = request.getCookies();
        if (loginCookies!=null){
            //空指针
            for (int i =0;i<loginCookies.length;i++){
                String temp = loginCookies[i].getName();
                if ("loginAcc".equals(temp)){
                    request.setAttribute("loginAcc",loginCookies[i].getValue());
                }
                else{
                    if ("loginPwd".equals(temp)){
                        request.setAttribute("loginPwd",loginCookies[i].getValue());
                    }
                }
            }
        }
        return "/login";
    }

    //用户执行登录操作
    //可以跳转到登录页面也可重定向到登录成功后的处理
    @RequestMapping("/gologin.do")
    public String goLogin(String acc, String pwd,String rem,  HttpServletRequest request, HttpSession session, HttpServletResponse response){
        //非空判断
        if (acc==null||pwd==null){
            request.setAttribute("msg","请重试");
            return "/login";
        }
        Result<Login> loginResult = loginService.queryLoginByAcc(acc, pwd);
        if (loginResult.getT()!=null){
            if (loginResult.getType()!=1){
                //非账号密码登录-
                //cookie登录
            }
            else {
                //账号密码登录
                //查看免登陆
                if (rem!=null){
                    Cookie loginAcc = new Cookie("loginAcc",loginResult.getT().getAcc());
                    Cookie loginPwd = new Cookie("loginPwd",loginResult.getT().getPwd());
                    response.addCookie(loginAcc);
                    response.addCookie(loginPwd);
                }
            }
            //设置session

            //
            //用户类别实现跳转
            String userType = null;
            switch (loginResult.getT().getType()){
                case 1:
                    userType = "user";
                    session.setAttribute("userId",loginResult.getT().getId());
                    break;
                case 2:
                    userType = "shop";
                    session.setAttribute("shopId",loginResult.getT().getId());
                    break;
                default:
                    new RuntimeException("登录异常");
            }
            //用户登录成功后的处理
            return "redirect:/"+userType+"/afterLogin.do";
        }
        else {
            //登录失败
            if (loginResult.getType()!=-1){
                //不是账号密码出错
                //账号出错
            }
            else{
                //清除密码，保留账号
                request.setAttribute("loginAcc",acc);
            }
            request.setAttribute("msg",loginResult.getMsg());
            return "/login";
        }
    }
    @Autowired
    LoginService loginService;
}
